home *** CD-ROM | disk | FTP | other *** search
- #include <genstub.c>
-
- DWORD TLSIndex = 0;
- BOOL fAbort;
-
- // Show progress without invoking waiting thread.
- VOID ShowRunningThread( HWND hWnd )
- {
- HDC hDC = GetDC( hWnd );
- TextOut(hDC, 0, 0, TlsGetValue( TLSIndex ), lstrlen( TlsGetValue( TLSIndex ) ) );
- ReleaseDC( hWnd, hDC );
- }
-
- // Thread procedure. This function just prints a message once every half-second.
- DWORD WINAPI ThreadProc( HWND hWnd )
- {
- LPVOID lpVoid = HeapAlloc( GetProcessHeap(), 0, 128 );
- wsprintf( lpVoid, "Instance %8lx is now running. ", GetCurrentThreadId() );
- TlsSetValue( TLSIndex, lpVoid );
- while ( !fAbort )
- {
- Sleep( 500 );
- ShowRunningThread( hWnd );
- Sleep( 0 );
- }
- HeapFree( GetProcessHeap(), 0, lpVoid );
- ExitThread( TRUE );
- }
-
- // Windows message procedure.
- LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CREATE:
- TLSIndex = TlsAlloc();
- break;
- case WM_DESTROY:
- TlsFree( TLSIndex );
- PostQuitMessage( 0 );
- break;
- case WM_COMMAND:
- switch ( LOWORD( wParam ) )
- {
- case IDM_TEST:
- {
- int i;
- HANDLE hThreads[4]; // Array of threads to wait for.
- DWORD dwChildID; // Child ID - discarded.
- fAbort = FALSE;
- for (i=0; i<4; i++)
- hThreads[i] = CreateThread( NULL, 0, ThreadProc,
- hWnd, 0, &dwChildID );
- // Wait half a minute.
- WaitForMultipleObjects( 4, &hThreads, TRUE, 28000 );
- fAbort = TRUE;
- WaitForMultipleObjects( 4, &hThreads, TRUE, 2000 );
- // Clean screen.
- InvalidateRect( hWnd, NULL, TRUE );
- UpdateWindow( hWnd );
- }
- break;
- case IDM_EXIT:
- DestroyWindow( hWnd );
- break;
- }
- break;
-
- default:
- return (DefWindowProc(hWnd, uMsg, wParam, lParam));
- }
- return (NULL);
- }